# -*- coding: utf-8 -*-
import os
import datetime
from urllib import unquote
from django.conf import settings
from mercurial import hg, ui

def site(request):
    return { 'site': settings.WIKI_SITE_NAME }

def changes(request):
    HG_PATH =os.path.join(settings.WIKI_REPOSITORY, '.hg')
    length = len(settings.WIKI_REPOSITORY) + 1
    changes = []
    for path, dirnames, filenames in os.walk(settings.WIKI_REPOSITORY):
        if path.startswith(HG_PATH):
            continue

        for filename in filenames:
            fullpath = os.path.join(path, filename)
            st = os.stat(fullpath)
            changes.append({'date': datetime.datetime.fromtimestamp(st.st_mtime),
                            'file': unquote(fullpath[length:]).decode('utf8'),
                            })
    changes.sort(lambda a, b: cmp(b['date'], a['date']))
    return { 'changes': changes }
